#!/usr/bin/env python
#coding=utf-8

import db, settings,utils,os

csvdir = settings.root + settings.history

def get_data_from_csv(fn, log):
    result = []
    
    exchange_days = []
    
    code = fn.split('.') [0]
    abs_fn = csvdir +  '/'  + fn
    
    f = open(abs_fn, 'r')
    lines = f.readlines()
    ls =[]
    for l in lines[1:]: 
        llist =l[:-1].split(',')
        ls.append(llist)
    ls.reverse()

    for r in ls:
        
        data = {}
        data['code'] = code
        data['date'] = r[0]
        data['open'] = r[1]
        data['high'] = r[2]
        data['low'] = r[3]
        data['close'] = r[4]
        data['volume'] = r[5]
        data['adj_close'] = r[6]                
        
        try:
            data['change'] =  (float(r[4]) - pre_close) * 100 /pre_close
        except UnboundLocalError:
            text = "%s,its ipo day:%s"%(code, r[0])
            log.write(text)
            log.flush()
            
        pre_close = float(r[4])
        result.append(data)
        exchange_days.append(r[0])
    
    return result,exchange_days

import os
ipo_log = open(settings.root + settings.log + "ipo.log", 'w')
con = utils.get_db_connector()
for fs in os.listdir(csvdir):
    data,days = get_data_from_csv(fs , ipo_log)
    db.insert_price_many(con, data, 500)
    db.daylist2db(con,fs.split('.')[0], days)
con.commit()
log.close()
